"use strict";
cc._RF.push(module, '7d4eegx+ZRMBJhrh9HYejy4', 'LevelBase');
// scripts/Game/Base/LevelBase.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../../engines/gameEngine");
var gameMode_json_1 = require("../../engines/managers/gameContent/gameMode_json");
var userInterface_1 = require("../../engines/services/userContent/userInterface");
var Enum_1 = require("../Enum");
var MainRoot_1 = require("../MainRoot");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var NormalLevelBase = /** @class */ (function (_super) {
    __extends(NormalLevelBase, _super);
    function NormalLevelBase() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.levelInfo = null;
        _this.title = null;
        _this.tips = null;
        _this.hadEnd = false;
        _this.m_mode = gameMode_json_1.GAME_MODE.NORMAL;
        return _this;
    }
    NormalLevelBase.prototype.onLoad = function () {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchScene.bind(this));
        gameEngine_1.default.on(Enum_1.GAME_EVENT.GAME_PAUSE, this.pauseGame.bind(this));
        gameEngine_1.default.on(Enum_1.GAME_EVENT.GAME_RESUME, this.resumeGame.bind(this));
        gameEngine_1.default.on(Enum_1.GAME_EVENT.ON_TIPS_GOT, this.onGetTip.bind(this));
        gameEngine_1.default.on(Enum_1.GAME_EVENT.ON_ANSWER_GOT, this.onGetAnswer.bind(this));
    };
    NormalLevelBase.prototype.getLevelInfo = function () {
        return this.levelInfo;
    };
    NormalLevelBase.prototype.onGetAnswer = function () {
    };
    NormalLevelBase.prototype.onGetTip = function () {
    };
    NormalLevelBase.prototype.pauseGame = function () { };
    NormalLevelBase.prototype.resumeGame = function () { };
    NormalLevelBase.prototype.touchScene = function (e) {
        var pos = e.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos);
        // let node = cc.instantiate(this.md);
        // mainRoot.UIRoot.addChild(node);
        // node.setPosition(pos);
        // node.getComponent(comSpSkeleton).playAnimation("animation").start(false).onComplete(1, () => {
        //     node.removeFromParent();
        // });
    };
    NormalLevelBase.prototype.Init = function (info) {
        return __awaiter(this, void 0, void 0, function () {
            var inGamePrams;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.levelInfo = info;
                        this.m_mode = info.mode;
                        this.tips = info.tips;
                        inGamePrams = {
                            mode: this.m_mode,
                            levelInfo: this.levelInfo,
                            title: info.title,
                            tips: info.tips,
                            hideTitle: info.hideTitle
                        };
                        gameEngine_1.default.on(Enum_1.GAME_EVENT.ON_BTN_TIPS, this.OnTips.bind(this));
                        return [4 /*yield*/, MainRoot_1.default.UISystem.OpenPage(Enum_1.PAGE.INGAMEPAGE, inGamePrams)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    NormalLevelBase.prototype.start = function () {
        var _this = this;
        MainRoot_1.default.UISystem.ClosePage(Enum_1.PAGE.TIPSPAGE);
        this.scheduleOnce(function () {
            var node = _this.node.getChildByName("GuangGuang");
            cc.tween(node).to(1, { opacity: 0 }).call(function () { node.active = false; }).start();
        }, 0.5);
    };
    NormalLevelBase.prototype.OnTips = function () {
        cc.audioEngine.pauseAll();
        var rewardFun = null;
        // console.error(this.levelInfo.reward);
        if (this.levelInfo.reward) {
            rewardFun = function () {
                console.warn("!!!没有");
                if (!MainRoot_1.default.GotTips) {
                    gameEngine_1.services().userSrv.setCoins(1, true);
                    gameEngine_1.modules().uiControlModule.showGetRewarded(userInterface_1.REWARD_TYPE.COIN, 1);
                }
            };
        }
        var tipsPrams = {
            mode: this.m_mode,
            levelInfo: this.levelInfo,
            tips: this.tips,
            onReward: rewardFun,
            onClose: function () {
                cc.audioEngine.resumeAll();
            }
        };
        MainRoot_1.default.UISystem.OpenPage(Enum_1.PAGE.TIPSPAGE, tipsPrams);
    };
    NormalLevelBase.prototype.OffTouchMove = function (node, event) {
        // console.log("quxiao",node.name);
        switch (event) {
            case cc.Node.EventType.TOUCH_MOVE:
                node.off(cc.Node.EventType.TOUCH_MOVE);
                break;
            case cc.Node.EventType.TOUCH_END:
            case cc.Node.EventType.TOUCH_CANCEL:
                node.off(cc.Node.EventType.TOUCH_END);
                node.off(cc.Node.EventType.TOUCH_CANCEL);
                break;
            default:
                node.off(cc.Node.EventType.TOUCH_MOVE);
                node.off(cc.Node.EventType.TOUCH_END);
                node.off(cc.Node.EventType.TOUCH_CANCEL);
                break;
        }
    };
    NormalLevelBase.prototype.RegisterTouchMove = function (node, registerFun, releaseFun) {
        var touchDir = cc.v2();
        var touchDir1 = cc.v2();
        var lastRatio = 1;
        var moveFun = function (event) {
            var startDir = null;
            var realDir = null;
            var touches = event.getTouches();
            // console.warn(touches.length);
            touchDir = touches[0].getLocation().sub(touches[0].getStartLocation());
            if (touches.length < 2) {
                registerFun(Enum_1.TOUCH_TYPE.SINGLE, touchDir, touches[0].getDelta(), node, event);
                return;
            }
            touchDir1 = touches[1].getLocation().sub(touches[1].getStartLocation());
            var cosAngle = touchDir.dot(touchDir1) / (touchDir.mag() * touchDir1.mag());
            if (cosAngle > 0.5 && cosAngle <= 1) {
                registerFun(Enum_1.TOUCH_TYPE.DOUBLE_SLIDER, touchDir1, touches[0].getDelta().scale(cc.v2(0.8, 0.8)), node, event);
                return;
            }
            startDir = touches[0].getStartLocation().sub(touches[1].getStartLocation());
            realDir = touches[0].getLocation().sub(touches[1].getLocation());
            var ratio = realDir.mag() / startDir.mag();
            registerFun(Enum_1.TOUCH_TYPE.DOUBLE_ZOOM, cc.v2(ratio, ratio), cc.v2(ratio - lastRatio, ratio - lastRatio), node, event);
            lastRatio = ratio;
        };
        var endFun = function (event) {
            lastRatio = 1;
            releaseFun && releaseFun(event);
        };
        node.on(cc.Node.EventType.TOUCH_MOVE, moveFun, this);
        node.on(cc.Node.EventType.TOUCH_END, endFun, this);
        node.on(cc.Node.EventType.TOUCH_CANCEL, endFun, this);
    };
    NormalLevelBase.prototype.Win = function (time, jyIndex, userStr) {
        var _this = this;
        if (this.hadEnd) {
            return;
        }
        this.hadEnd = true;
        // this.ShowObj("Xz_Zq", (node: cc.Node) => {
        //     // managers().soundMgr.playSound("huaquan");
        //     node.setPosition(position);
        // });
        // mainRoot.UISystem.ClosePage(PAGE.TIPSPAGE);
        var delayTime = 3.5;
        if (time) {
            delayTime = time;
        }
        this.scheduleOnce(function () { return __awaiter(_this, void 0, void 0, function () {
            var winTest, winPrams;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        MainRoot_1.default.UISystem.ClosePage(Enum_1.PAGE.TIPSPAGE);
                        gameEngine_1.managers().soundMgr.playSound('mainSound/win');
                        winTest = null;
                        // if (isUseString) {
                        winTest = this.levelInfo.winText;
                        // }
                        if (userStr) {
                            winTest = userStr;
                        }
                        winPrams = {
                            mode: this.m_mode,
                            levelInfo: this.levelInfo,
                            winText: winTest,
                            index: this.levelInfo.level,
                            jyInde: jyIndex
                        };
                        gameEngine_1.managers().soundMgr.stopAllSound();
                        return [4 /*yield*/, MainRoot_1.default.UISystem.OpenPage(Enum_1.PAGE.WINPAGE, winPrams)];
                    case 1:
                        _a.sent();
                        MainRoot_1.default.LevelSystem.AddLevel(this.m_mode);
                        console.warn('重置1');
                        MainRoot_1.default.GotTips = false;
                        MainRoot_1.default.GotAnswer = false;
                        return [2 /*return*/];
                }
            });
        }); }, delayTime);
    };
    NormalLevelBase.prototype.Lose = function (time) {
        var _this = this;
        if (this.hadEnd) {
            return;
        }
        this.hadEnd = true;
        var delayTime = 2;
        if (time) {
            delayTime = time;
        }
        // this.ShowObj("Xz_Cw", (node: cc.Node) => {
        //     if(position)
        //         node.setPosition(position);
        // });
        this.scheduleOnce(function () {
            gameEngine_1.managers().soundMgr.playSound('mainSound/inLevel');
            // let losePrams: ILosePagePrams = {
            //     mode: this.m_mode,
            //     loseText: this.levelInfo.loseText,
            //     tips: this.levelInfo.tips,
            //     index: this.levelInfo.level
            // };
            // mainRoot.UISystem.OpenPage(PAGE.LOSEPAGE, losePrams);
            MainRoot_1.default.LevelSystem.LoadLevel(MainRoot_1.default.LevelSystem.GetSignleLevelInfo(_this.m_mode).bundleName, _this.m_mode, null, false);
            MainRoot_1.default.UISystem.CloseAllPage();
        }, delayTime);
    };
    NormalLevelBase.prototype.getHasEndGame = function () {
        return this.hadEnd;
    };
    /**
     * 展示一个预制体到场上
     * @param name 预制体的名字，预制体需要位于GameframeBundle/Obj下
     * @param callback 加载完成回调,加载出的节点为参数
     */
    NormalLevelBase.prototype.ShowObj = function (name, callback) {
        var _this = this;
        if (name == "Xz_Cw") {
            // managers().soundMgr.playSound("cuo");
        }
        return new Promise(function (resolve) {
            MainRoot_1.default.frameBundle.load("Obj/" + name, function (err, prefab) { return __awaiter(_this, void 0, void 0, function () {
                var node, _a;
                return __generator(this, function (_b) {
                    switch (_b.label) {
                        case 0:
                            if (err) {
                                console.error(err);
                                return [2 /*return*/];
                            }
                            node = cc.instantiate(prefab);
                            if (node) {
                                this.node && (this.node.addChild(node));
                                node.scale = 0.1;
                                cc.tween(node).to(0.3, { scale: 1 }, { easing: 'backOut' }).start();
                            }
                            // node.setPosition(obj?obj.getPosition():cc.v2());
                            // managers().soundMgr.playSound("huaquan");
                            _a = callback;
                            if (!_a) 
                            // node.setPosition(obj?obj.getPosition():cc.v2());
                            // managers().soundMgr.playSound("huaquan");
                            return [3 /*break*/, 2];
                            return [4 /*yield*/, callback(node)];
                        case 1:
                            _a = (_b.sent());
                            _b.label = 2;
                        case 2:
                            // node.setPosition(obj?obj.getPosition():cc.v2());
                            // managers().soundMgr.playSound("huaquan");
                            _a;
                            resolve(node);
                            return [2 /*return*/];
                    }
                });
            }); });
        });
    };
    /**
     *  更新标题
     * @param title 标题内容
     */
    NormalLevelBase.prototype.reNewTiTle = function (title) {
    };
    NormalLevelBase.prototype.onDestroy = function () {
        cc.audioEngine.stopAllEffects();
    };
    NormalLevelBase = __decorate([
        ccclass
    ], NormalLevelBase);
    return NormalLevelBase;
}(cc.Component));
exports.default = NormalLevelBase;

cc._RF.pop();